<?php
class user_model extends MY_model
{
    public $id;
    public $username;
    public $password;
    public $user_group_code;
    public $note;
    public $modified_date = 0;
    public $authorities;

    public static $diamond_type = 'DIAMOND_STAFF';
    public static $livingnext_type = 'LIVING_NEXT_STAFF';
    public static $admin_type = 'SUPER_ADMIN';


    public function __construct()
    {
        parent::__construct();
        $this->table_name = 'user';
    }

    public static function get_elements()
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM user");
        return self::to_domain_models($query->result());
    }

    public static function get_elements_created_by($created_by)
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM user WHERE authorities = ?",array($created_by));
        return self::to_domain_models($query->result());
    }

    public static function get_element_by_id($id)
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM user WHERE id = ?",array($id));
        return self::to_domain_model($query->row());
    }

    public static function get_element_by_username_and_password($username,$password)
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM user WHERE username = ? AND password = ?",array($username,$password));
        return self::to_domain_model($query->row());
    }

    public static function get_elements_by_user_group_code($code)
    {
        $query = MY_model::getDbInstance()->query("SELECT * FROM user WHERE user_group_code = ?",array($code));
        return self::to_domain_models($query->result());
    }

    public static function get_all_type_user()
    {
        $query = MY_model::getDbInstance()->query("SELECT DISTINCT user_group_code FROM user");
        return self::to_domain_models($query->result());
    }

    public static function get_count_staff_number($type)
    {
        $query = MY_model::getDbInstance()->query("SELECT COUNT(*) AS count FROM user WHERE user_group_code = ?",array($type));
        return $query->row()->count;
    }

    // -------------------------------------------------------------------------------- PRIVATE METHOD

    // Private method
    private static function to_domain_models($array)
    {
        $result = array();
        foreach($array as $item)
        {
            $result[] = user_model::to_domain_model($item);
        }
        return $result;
    }

    // Private method
    private static function to_domain_model($item)
    {
        $model = new user_model();
        $model->id = isset($item->id) ? $item->id : '' ;
        $model->username = isset($item->username) ? $item->username : '' ;
        $model->password = isset($item->password) ? $item->password : '' ;
        $model->user_group_code = isset($item->user_group_code) ? $item->user_group_code : '';
        $model->note = isset($item->note) ? $item->note : '';
        $model->modified_date = isset($item->modified_date) ? $item->modified_date: '0';
        $model->authorities = isset($item->authorities) ? $item->authorities: '';
        return $model;
    }

    protected function to_array()
    {
        $data_arr = array(
            'username' => $this->username,
            'password' => $this->password,
            'user_group_code' => $this->user_group_code,
            'note' => $this->note,
            'modified_date' => $this->modified_date,
            'authorities' => $this->authorities
        );

        return $data_arr;
    }
}